package com.jingtai.shop.controller.goods;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jingtai.common.template.ResultMapInfo;
import com.jingtai.shop.common.template.ShopBaseController;
import com.jingtai.shop.service.goods.ShopCartService;
import com.jingtai.shop.webfacade.bo.shopcar.*;
import com.jingtai.shop.webfacade.vo.shopcar.list.TotalShopCarVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(tags = "商城-购物车")
@RestController
@RequestMapping("/shop/shopCart")
@CrossOrigin(origins = "*")
public class ShopCartController extends ShopBaseController {

    @Resource
    private ShopCartService shopCartService;

    /**
     * 添加购物车
     */
    @ApiOperation(value = "添加购物车")
    @ApiOperationSupport(ignoreParameters = {"bo.platform", "bo.version"})
    @PostMapping("/saveShopCart")
    public Map<String, Object> saveShopCart(@RequestBody ShopCarAddBO bo) {
        bo.validate();
        shopCartService.saveShopCart(bo.getGoodsId(), bo.getSkuCode(), bo.getQuantity(), getUserId());
        return returnResultMap(ResultMapInfo.OPERATE_SUCCESS);
    }

    /**
     * 清空购物车
     */
    @ApiOperation(value = "清空购物车")
    @PostMapping("/clearShopCart")
    public Map<String, Object> clearShopCart() {
        shopCartService.clearShopCart(getUserId());
        return returnResultMap(ResultMapInfo.OPERATE_SUCCESS);
    }

    /**
     * 删除购物车商品
     */
    @ApiOperation(value = "删除购物车商品")
    @ApiOperationSupport(ignoreParameters = {"bo.platform", "bo.version"})
    @PostMapping("/removeShopCart")
    public Map<String, Object> removeShopCart(@RequestBody ShopCarRemoveBO bo) {
        bo.validate();
        shopCartService.removeShopCart(bo, getUserId());
        return returnResultMap(ResultMapInfo.OPERATE_SUCCESS);
    }

    /**
     * 用户购物车列表
     */
    @ApiOperation(value = "用户购物车列表")
    @PostMapping("/listUserShopCart")
    public Map<String, Object> listUserShopCart() {
        List<TotalShopCarVO> shopCarVOS = shopCartService.listUserShopCart(getUserId());
        return returnResultMap(ResultMapInfo.GETSUCCESS, shopCarVOS);
    }

    /**
     * 修改购物车商品信息
     */
    @ApiOperation(value = "修改购物车商品信息")
    @ApiOperationSupport(ignoreParameters = {"bo.platform", "bo.version"})
    @PostMapping("/updateShopCart")
    public Map<String, Object> updateShopCart(@RequestBody ModifyShopCarQuantityBO bo) {
        bo.validate();
        shopCartService.updateShopCart(bo, getUserId());
        return returnResultMap(ResultMapInfo.OPERATE_SUCCESS);
    }

    /**
     * 购物车提交订单
     */
    @ApiOperation(value = "购物车提交订单")
    @ApiOperationSupport(ignoreParameters = {"bo.platform", "bo.version"})
    @PostMapping("/submitOrder")
    public Map<String, Object> submitOrder(@RequestBody SubmitOrderTotalBO bo) {
        bo.validate();
        //boolean submitOrder = shopCartService.submitOrder(bo, getUserId());
        boolean submitOrder = shopCartService.submitOrder(bo, 1L);
        return returnResultMap(submitOrder ? ResultMapInfo.SUBMITSUCCESS : ResultMapInfo.SUBMITFAIL);
    }

    /**
     * 根据skucode查询商品
     */
    @ApiOperation(value = "根据skucode查询商品")
    @ApiOperationSupport(ignoreParameters = {"bo.platform", "bo.version"})
    @PostMapping("/selectGoodsBySkuCode")
    public Map<String, Object> selectGoodsBySkuCode(@RequestBody SkuBO bo) {
        List<TotalShopCarVO> shopCarVOS = shopCartService.selectGoodsBySkuCode(bo.getSkuCode(),bo.getType(),bo.getGoodsId(),bo.getQuantity(),getUserId());
        return returnResultMap(ResultMapInfo.GETSUCCESS, shopCarVOS);
    }
}
